package com.zwq.zwqjavanotes.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @ClassName DateUtil
 * @Description 日期操作工具类
 * @Author @zwq O_o
 * @Date 2025/5/8 11:42
 * @Version 1.0
 */
public class DateUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * @return
     * @return java.lang.String[]
     * @Author @zwq O_o
     * @Description //获取T-1 DAY 开始时间和结束时间
     * @Date 11:46 2025/5/8
     **/
    public static String[] getPreviousDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        //设置开始时间
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String startTime = sdf.format(calendar.getTime());
        //设置结束时间
        calendar.add(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String endTime = sdf.format(calendar.getTime());

        return new String[]{startTime, endTime};
    }

    /**
     * @return
     * @Author @zwq O_o
     * @Description //获取上周一至周天
     * @Date 11:50 2025/5/8
     * @return
     * @return java.lang.String[]
     **/
    public static String[] getPreviousWeek() {
        Calendar calendar = Calendar.getInstance();
        //设置一周的第一天为星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //切换上周
        calendar.add(Calendar.WEEK_OF_YEAR, -1);
        //设置开始时间为当周的周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String startTime = sdf.format(calendar.getTime());

        //设置结束时间为当周的周日
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String endTime = sdf.format(calendar.getTime());

        return new String[]{startTime, endTime};
    }

    /**
     * @return
     * @return java.lang.String[]
     * @Author @zwq O_o
     * @Description //获取上月第一天和最后一天
     * @Date 11:53 2025/5/8
     **/
    public static String[] getPreviousMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        //设置开始时间为当月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String startTime = sdf.format(calendar.getTime());
        //设置结束时间为当月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String endTime = sdf.format(calendar.getTime());

        return new String[]{startTime, endTime};
    }
    /**
     * @Author @zwq O_o
     * @Description //获取去年第一天和最后一天
     * @Date 11:55 2025/5/8
     * @return 
     * @return java.lang.String[]
     **/
    public static String[] getPreviousYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);

        //设置开始时间为当年第一天
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String startTime = sdf.format(calendar.getTime());
        //设置结束时间为当年最后一天
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String endTime = sdf.format(calendar.getTime());
        
        return new String[]{startTime, endTime};
    }
}
